﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.SMS.Common;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Gateway.Host
{
    public class SendMsgTask : Task
    {
        protected ISMSProvider _smsProvider;
        protected IServiceConfigProvider _configProvider;
        protected IDataProvider _databaseProvider;
        public SendMsgTask(
            ISMSProvider smsProvider,
            IServiceConfigProvider configProvider,
            IDataProvider databaseProvider
            )
        {
            _smsProvider = smsProvider;
            _configProvider = configProvider;
            _databaseProvider = databaseProvider;
        }
        protected override int TaskEnable
        {
            get
            {
                return _configProvider.SendMsgTaskEnable;
            }
            set
            {
                TaskEnable = value;
            }
        }
        protected override int TaskSpan
        {
            get
            {
                return _configProvider.SendMsgTaskSpan;
            }
            set
            {
                TaskSpan = value;
            }
        }
        protected override void ExecuteCore()
        {
            while (_scanAble && TaskEnable == 1)
            {
                ScanToBeSendedCore();
                System.Threading.Thread.Sleep(
                    TaskSpan < GlobalFactory.TaskSpanBorder ? GlobalFactory.TaskSpanBorder : _configProvider.SendMsgTaskSpan);
            }
        }

        private void ScanToBeSendedCore()
        {
            DBLog.Debug("Enter ScanToBeSendedCore");
            var msgs = _databaseProvider.GetMsgToBeSended();
            if (msgs == null)
            {
                return;
            }
            List<KeyandMessageId> changeSuccessInfo = new List<KeyandMessageId>();
            List<KeyandMessageId> changeFailedInfo = new List<KeyandMessageId>();
            foreach (var msg in msgs)
            {
                SMSResult msgResult = _smsProvider.SendMsg(msg.ToMobile, msg.Content);
                if (msgResult == null || msgResult.Status != SendStatus.OK)
                {
                    KeyandMessageId info = new KeyandMessageId();
                    info.MsgIdInDbPrimaryKey = msg.MsgIdInDbPrimaryKey;
                    info.MessageId = null;
                    changeFailedInfo.Add(info);
                }

                else 
                {
                    KeyandMessageId info = new KeyandMessageId();
                    info.MsgIdInDbPrimaryKey = msg.MsgIdInDbPrimaryKey;
                    info.MessageId = msgResult.MessageId;
                    changeSuccessInfo.Add(info);
                }
                
                DBLog.Debug("ScanSendingCore MsgIdInDbPrimaryKey:" + msg.MsgIdInDbPrimaryKey);
            }
            List<Email> pushEmail1 = new List<Email>();
            pushEmail1 = _databaseProvider.SetMsgSendingSuccess(changeSuccessInfo);
            List<Email> pushEmail2 = new List<Email>();
            pushEmail2 = _databaseProvider.SetMsgSendingFailed(changeFailedInfo);
            var sqlEmails = GlobalFactory.CreateEmailDatabaseProvider();
            try
            {
                if (pushEmail1 != null && pushEmail1.Count() != 0)
                {
                    if (pushEmail2 != null && pushEmail2.Count() != 0)
                        pushEmail1.AddRange(pushEmail2);
                    foreach (var email in pushEmail1)
                    {
                        string mail = email.Content.Replace("tobechange", _configProvider.WebUrl);
                        mail = mail.Replace("leftcall", "{");
                        email.Content = mail.Replace("rightcall", "}");
                    }
                    sqlEmails.SendEmail(pushEmail1);
                }
                else if (pushEmail2 != null && pushEmail2.Count() != 0)
                {
                    if (pushEmail1 != null && pushEmail1.Count() != 0)
                        pushEmail2.AddRange(pushEmail1);
                    DBLog.Debug("url" + _configProvider.WebUrl);
                    foreach (var email in pushEmail2)
                    {
                        string mail = email.Content.Replace("tobechange", _configProvider.WebUrl);
                        mail = mail.Replace("leftcall", "{");
                        email.Content = mail.Replace("rightcall", "}");
                    }
                    sqlEmails.SendEmail(pushEmail2);
                }
            }
            catch (Exception ex)
            {
                DBLog.Error(ex.Message);
            }
            DBLog.Debug("leaving ScanToBeSendedCore");
        }
            

            
        
    }
}
